﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace GreenArrow
{
    partial class PickerForm : Form
    {
        public PickerResult result;
        public PickerStatus status;
        public Cursor cursorCross;
        public Cursor cursorPoint;

        public struct IconInfo {
            public bool fIcon;
            public int xHotspot;
            public int yHotspot;
            public IntPtr hbmMask;
            public IntPtr hbmColor;
        }
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);

        [DllImport("user32.dll")]
        static extern IntPtr CreateIconIndirect(ref IconInfo icon);

        static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot) {
            IntPtr ptr = bmp.GetHicon();
            IconInfo tmp = new IconInfo();
            GetIconInfo(ptr, ref tmp);
            tmp.xHotspot = xHotSpot;
            tmp.yHotspot = yHotSpot;
            tmp.fIcon = false;
            ptr = CreateIconIndirect(ref tmp);
            return new Cursor(ptr);
        }

        public PictureBox ImageBox {
            get {
                return pickerBox;
            }
        }
        public PickerForm(PickerStatus _status, PickerResult _result)
        {
            result = _result;
            status = _status;
            status.form = this;
            LoadCursors();
            //status.Init();
            InitializeComponent();
        }
        void LoadCursors() {
            LoadCursorCross();
            LoadCursorPoint();
        }
        void LoadCursorCross() {
            Bitmap bitmap = new Bitmap(250, 40);
            Graphics g = Graphics.FromImage(bitmap);
            using (Font f = new Font(FontFamily.GenericSansSerif, 10))
            {
                g.DrawLine(Pens.Green, 0, 10, 20, 10);
                g.DrawLine(Pens.Green, 10, 0, 10, 20);
                g.DrawString("Select marking rectangle", f, Brushes.Green, 20, 20);
            }
            cursorCross = CreateCursor(bitmap, 10, 10);
            bitmap.Dispose();
        }
        void LoadCursorPoint() {
            Bitmap bitmap = new Bitmap(250, 40);
            Graphics g = Graphics.FromImage(bitmap);
            using (Font f = new Font(FontFamily.GenericSansSerif, 10))
            {
                g.DrawLine(Pens.Green, 0, 0, 20, 20);
                g.DrawLine(Pens.Green, 20, 0, 0, 20);
                g.DrawEllipse(Pens.Green, 0, 0, 20, 20);
                g.DrawString("Select clicking point", f, Brushes.Green, 20, 20);
            }
            cursorPoint = CreateCursor(bitmap, 10, 10);
            bitmap.Dispose();
        }
        private void PickerForm_Load(object sender, EventArgs e)
        {
            Left = 0;
            Top = 0;
            status.Init();
        }

        private void pickerBox_DoubleClick(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pickerBox_MouseDown(object sender, MouseEventArgs e)
        {
            status.MouseDown(e);
        }

        private void pickerBox_MouseUp(object sender, MouseEventArgs e)
        {
            status.MouseUp(e);
        }

        private void pickerBox_MouseMove(object sender, MouseEventArgs e)
        {
            status.MouseMove(e);
        }

        private void pickerBox_MouseClick(object sender, MouseEventArgs e)
        {
            status.MouseClick(e);
        }

        private void PickerForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            status.KeyPress(e);
        }

    }
}
